﻿/********************************************
 * 
 * Jim Trebbien
 * 
 * *****************************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Model
{
    public class Inputreader
    {
       

        private Queue<char> txtinput;
        private List<string> leksemer = new List<string>();


        public Inputreader(Queue<char> txtinput)
        {
            this.txtinput = txtinput;
        }

        public void Scan()
        {
            while (txtinput.Peek() != '@')
            {
                ScanToken();
            }
        }

        public List<string> GetLeksemer()
        {
            return leksemer;
        }


        public void ScanToken()
        {

            if (txtinput.Peek() == '#')
                ScanForRute();
            else if (char.IsLetter(txtinput.Peek()))
                ScanForBynavn();
            else if (char.IsDigit(txtinput.Peek()))
                ScanForTidspunkt();
            else if (char.IsWhiteSpace(txtinput.Peek()))
                txtinput.Dequeue();

            else throw new Exception("Token fejl: " + txtinput.Peek());
        }

        public void ScanForRute()
        {
            string rute = ""; 
            txtinput.Dequeue();

            while (char.IsDigit(txtinput.Peek()))
            {
                rute += txtinput.Peek().ToString();
                txtinput.Dequeue();
            }
            leksemer.Add(rute);
            //Console.WriteLine("Rutenr genkendt.");
            
        }

        public void ScanForBynavn()
        {

            string bynavn = "";

            do
            {
                bynavn += txtinput.Peek().ToString();
                txtinput.Dequeue();

            } while (char.IsLetter(txtinput.Peek()));
            leksemer.Add(bynavn);
            //Console.WriteLine("Bynavn genkendt.");
           

        }

        public void ScanForTidspunkt()
        {

            string tidspunkt = "" + txtinput.Peek(); 

            txtinput.Dequeue();
            if (!char.IsDigit(txtinput.Peek()))
                throw new Exception("fejl 0 ScanForTidspunkt");
            tidspunkt += txtinput.Peek().ToString();
            txtinput.Dequeue();
            if(txtinput.Peek() != ':')
                throw new Exception("fejl 1 ScanForTidspunkt");
            tidspunkt += txtinput.Peek().ToString();
            txtinput.Dequeue();
            if (!char.IsDigit(txtinput.Peek()))
                throw new Exception("fejl 2 ScanForTidspunkt");
            tidspunkt += txtinput.Peek().ToString();
            txtinput.Dequeue();
            if (!char.IsDigit(txtinput.Peek()))
                throw new Exception("fejl 3 ScanForTidspunkt");
            tidspunkt += txtinput.Peek().ToString();
            txtinput.Dequeue();
            leksemer.Add(tidspunkt);
            //Console.WriteLine("Tidspunkt genkendt.");

        }
    }


}
